//
//  HLSearchUserCell.m
//  YueYue
//
//  Created by hulu001 on 2023/6/16.
//

#import "HLSearchUserCell.h"

@interface HLSearchUserCell ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UIImageView *countryView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) QMUIButton *ageView;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) QMUIButton *attentionButton;
@property (nonatomic, strong) QMUIButton *cancelAttentionButton;
@property (nonatomic, strong) QMUIButton *cancelBlackButton;
@property (nonatomic, strong) QMUIButton *chatButton;

@end

@implementation HLSearchUserCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 30;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.image = [HLUIHelper avatarPlaceholder];
    [self.contentView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(60);
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(12);
        make.bottom.mas_equalTo(-12);
    }];
    self.countryView = [[UIImageView alloc] init];
    _countryView.image = UIImageMake(@"country_cn_icon");
    _countryView.layer.masksToBounds = YES;
    _countryView.layer.cornerRadius = 7;
    [self.contentView addSubview:_countryView];
    [_countryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(_avatarView);
        make.size.mas_equalTo(14);
    }];
    
    
    self.nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.textColor = HLTextColor1;
    _nicknameLabel.text = @"聊人生阿姨有没有啊";
    _nicknameLabel.font = UIFontMediumMake(15);
    [self.contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarView.mas_right).offset(8);
        make.top.equalTo(_avatarView).offset(8);
        make.right.mas_equalTo(-104);
    }];
    
    self.ageView = [HLUIHelper sexAgeIconViewWithSex:@"0" age:@"22"];
    [self.contentView addSubview:_ageView];
    [_ageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_nicknameLabel);
        make.left.equalTo(_nicknameLabel.mas_right).offset(6);
        make.right.mas_equalTo(-104);
    }];
    
    self.contentLabel = [[UILabel alloc] init];
    _contentLabel.font = UIFontMake(12);
    _contentLabel.textColor = HLTextColor1;
    _contentLabel.numberOfLines = 1;
    _contentLabel.text = @"错误的决定比没有决定要好";
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.bottom.equalTo(_avatarView).offset(-10);
        make.right.mas_equalTo(-104);
    }];
    
    
    self.attentionButton = [[QMUIButton alloc] init];
    [_attentionButton setTitle:@"关注" forState:UIControlStateNormal];
    [_attentionButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_attentionButton setCornerRadius:14];
    [_attentionButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
    [_attentionButton setBackgroundColor:HLMainColor];
    [_attentionButton.titleLabel setFont:UIFontMediumMake(12)];
    [_attentionButton addTarget:self action:@selector(attentionAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_attentionButton];
    [_attentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
    
    self.cancelAttentionButton = [[QMUIButton alloc] init];
    [_cancelAttentionButton setTitle:@"取消关注" forState:UIControlStateNormal];
    [_cancelAttentionButton setTitleColor:HLTextColor2 forState:UIControlStateNormal];
    [_cancelAttentionButton setCornerRadius:14];
    [_cancelAttentionButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
    [_cancelAttentionButton setBackgroundColor:HLBackgroundColor];
    [_cancelAttentionButton.titleLabel setFont:UIFontMediumMake(12)];
    [_cancelAttentionButton addTarget:self action:@selector(attentionAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_cancelAttentionButton];
    [_cancelAttentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
}


- (void)setModel:(HLSearchUserModel *)model
{
    _model = model;
    
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
    _nicknameLabel.text = model.nickName;
    _contentLabel.text = [NSString stringWithFormat:@"ID: %@", model.userYueId];
    [_ageView setTitle:model.age forState:UIControlStateNormal];
    _attentionButton.hidden = YES;
    _cancelAttentionButton.hidden = YES;
    
    if (!model.followType) {
        // 未关注
        _attentionButton.hidden = NO;
    } else if ([model.followType isEqualToString:@"0"]) {
        // 仅关注
        _cancelAttentionButton.hidden = NO;
    } else if ([model.followType isEqualToString:@"1"]) {
        // 已互关
        _cancelAttentionButton.hidden = NO;
    } else {
        _attentionButton.hidden = NO;
    }
}

- (void)attentionAction
{
    MJWeakSelf;
    BOOL hasFollow = [_model.followType isEqualToString:@"0"];
    [HLUIHelper followWithUserId:_model.userId follow:hasFollow callback:^(BOOL isConfirm) {
        if (isConfirm){
            weakSelf.model.followType = hasFollow ? @"1":@"0";
            [weakSelf.qmui_tableView reloadData];
        }
    }];
}



@end
